import requests,aiohttp,asyncio,re,aiofiles,json

def down(url):
    res = requests.get(url=url)
    # print(res.text)

    return res

if __name__ == '__main__':
    url = 'http://www.niumatv.com/play/36807-1-1.html'
    res = down(url).text
    urlndex = ''.join(re.findall(r'"url":"(.*?)","url_next"',res)).replace('\/','/')
    res1 = down(urlndex).content
    # res3 = ''.join(re.findall("20220809(.*?)m3u8",res1))
    #https://s7.fsvod1.com/20220809/gEA8lv4k/1500kb/hls/index.m3u8
    #https://s7.fsvod1.com/20220809/gEA8lv4k/1500kb/hls/index.m3u8
    # print(res1)
    with open('../分手的决心.m3u8', 'wb') as f:
        f.write(res1)
    with open('../分手的决心.m3u8', mode='r', encoding='utf-8') as f:
        lines = f.readlines()
        indexurl = ''.join('https://s7.fsvod1.com' + lines[2]).strip()
        # print(indexurl)
    resfut = down(indexurl).content
        # print(resfut)
    with open('../分手的决心1.m3u8', mode='wb') as f:
        f.write(resfut)
    n =1
    with open('../分手的决心1.m3u8', mode='r', encoding='utf-8') as f:
        for line in f :
            line = line.strip()
            if line.startswith('#'):
                continue
            line = ''.join('https://s7.fsvod1.com'+line).strip()
            res3 = down(line)
            f = open(f"video/{n}.ts",mode='wb')
            f.write(res3.content)
            n += 1
            f.close()
            res3.close()
            print('success')

#https://s7.fsvod1.com/20220809/gEA8lv4k/1500kb/hls/index.m3u8
#https://s7.fsvod1.com/20220809/gEA8lv4k/1500kb/hls/index.m3u8